const connection = require('../app/database')
class CommentService {
  async create (userId, momentId, content){
    try {
      const statement = `INSERT INTO comment (user_id, moment_id, content) VALUES(?, ?, ?) `
      const [result] = await connection.execute(statement,[userId,momentId, content])
      return result 
    } catch (error) {
      console.log(err)
    }
  }
  async replay (userId, momentId, content, commentId){
    try {
      const statement = `INSERT INTO comment (user_id, moment_id, content,comment_id) VALUES(?, ?, ?,?) `
      const [result] = await connection.execute(statement,[userId,momentId, content,commentId])
      return result 
    } catch (error) {
      console.log(err)
    }
  }
  async update(commentId, content) {
    try {
      const statement = `UPDATE comment SET content = ? WHERE id = ?`
      const [result] =  await connection.execute(statement, [content, commentId])
      return result
    } catch (error) {
      console.log(error)
    }
  }
  async remove(commentId){
    try {
      const statement = `DELETE FROM comment  where id = ?`
      const [result] =  await connection.execute(statement, [commentId])
      return result
    } catch (error) {
      console.log(error)
    }
  }
  async getCommentByMomentId (momentId){
    const statement = `SELECT m.id, m.content, m.comment_id commentId, 
    JSON_OBJECT('id', u.id, 'name', u.name) commenter
    FROM comment m LEFT JOIN users u ON u.id = m.user_id WHERE moment_id = ?;`
    const [result] = await connection.execute(statement, [momentId])
    return result
  }
}

module.exports = new CommentService()